Developer Documentation

QuickTime 4 API Documentation

Inside Macintosh: Memory

Previous | Chapter Top | Chapter Contents | Next |

Setting Up the Application Heap

The Operating System automatically initializes your application's heap when your application is launched. To help prevent heap fragmentation, you should call the procedures in this section before you allocate any blocks of memory in your heap.

Use the MaxApplZone procedure to extend the application heap zone to the application heap limit so that the Memory Manager does not do so gradually as memory requests require. Use the MoreMasters procedure to preallocate enough blocks of master pointers so that the Memory Manager never needs to allocate new master pointer blocks for you.

MaxApplZone

To help ensure that you can use as much of the application heap zone as possible, call the MaxApplZone procedure. Call this once near the beginning of your program, after you have expanded your stack.

PROCEDURE MaxApplZone;

DESCRIPTION

The MaxApplZone procedure expands the application heap zone to the application heap limit. If you do not call MaxApplZone , the application heap zone grows as necessary to fulfill memory requests. The MaxApplZone procedure does not purge any blocks currently in the zone. If the zone already extends to the limit, MaxApplZone does nothing.

It is a good idea to call MaxApplZone once at the beginning of your program if you intend to maintain an effectively partitioned heap. If you do not call MaxApplZone and then call MoveHHi to move relocatable blocks to the top of the heap zone before locking them, the heap zone could later grow beyond these locked blocks to fulfill a memory request. If the Memory Manager were to allocate a nonrelocatable block in this new space, your heap would be fragmented.

ASSEMBLY-LANGUAGE INFORMATION

The registers on exit for MaxApplZone are

Registers on exit

D0

Result code

RESULT CODES

noErr

0

No error

MoreMasters

Call the MoreMasters procedure several times at the beginning of your program to prevent the Memory Manager from running out of master pointers in the middle of application execution. If it does run out, it allocates more, possibly causing heap fragmentation.

PROCEDURE MoreMasters;

DESCRIPTION

The MoreMasters procedure allocates another block of master pointers in the current heap zone. In the application heap, a block of master pointers consists of 64 master pointers, and in the system heap, a block consists of 32 master pointers. (These values, however, might change in future versions of system software.) When you initialize additional heap zones, you can specify the number of master pointers you want to have in a block of master pointers.

The Memory Manager automatically calls MoreMasters once for every new heap zone, including the application heap zone.

You should call MoreMasters at the beginning of your program enough times to ensure that the Memory Manager never needs to call it for you. For example, if your application never allocates more than 300 relocatable blocks in its heap zone, then five calls to the MoreMasters should be enough. It's better to call MoreMasters too many times than too few. For instance, if your application usually allocates about 100 relocatable blocks but might allocate 1000 in a particularly busy session, call MoreMasters enough times at the beginning of the program to accommodate times of greater memory use.

If you are forced to call MoreMasters so many times that it causes a significant slowdown, you could change the moreMast field of the zone header to the total number of master pointers you need and then call MoreMasters just once. Afterward, be sure to restore the moreMast field to its original value.

SPECIAL CONSIDERATIONS

Because MoreMasters allocates memory, you should not call it at interrupt time.

The calls to MoreMasters at the beginning of your application should be in the main code segment of your application or in a segment that the main segment never unloads.

ASSEMBLY-LANGUAGE INFORMATION

The registers on exit for MoreMasters are

Registers on exit

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

GetApplLimit

Use the GetApplLimit function to get the application heap limit, beyond which the application heap cannot expand.

FUNCTION GetApplLimit: Ptr;

DESCRIPTION

The GetApplLimit function returns the current application heap limit. The Memory Manager expands the application heap only up to the byte preceding this limit.

Nothing prevents the stack from growing below the application limit. If the Operating System detects that the stack has crashed into the heap, it generates a system error. To avoid this, use GetApplLimit and the SetApplLimit procedure to set the application limit low enough so that a growing stack does not encounter the heap.

The GetApplLimit function does not indicate the amount of memory available to your application.

ASSEMBLY-LANGUAGE INFORMATION

The global variable ApplLimit contains the current application heap limit.

SetApplLimit

Use the SetApplLimit procedure to set the application heap limit, beyond which the application heap cannot expand.

PROCEDURE SetApplLimit (zoneLimit: Ptr);
zoneLimit
A pointer to a byte in memory demarcating the upper boundary of the application heap zone. The zone can grow to include the byte preceding zoneLimit in memory, but no further.

DESCRIPTION

The SetApplLimit procedure sets the current application heap limit to zoneLimit . The Memory Manager then can expand the application heap only up to the byte preceding the application limit. If the zone already extends beyond the specified limit, the Memory Manager does not cut it back but does prevent it from growing further.

The zoneLimit parameter is not a byte count, but an absolute byte in memory. Thus, you should use the SetApplLimit procedure only with a value obtained from the Memory Manager functions GetApplLimit or ApplicationZone .

You cannot change the limit of zones other than the application heap zone.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for SetApplLimit are

Registers on entry

A0

Pointer to desired new zone limit

Registers on exit

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

SEE ALSO

To use SetApplLimit to expand the default size of the stack, see the discussion in "Changing the Size of the Stack" .


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next